package com.xj.api.bussiness.appUser.web;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.feilong.core.bean.ConvertUtil;
import com.xj.api.base.web.BaseController;
import com.xj.api.bussiness.appUser.service.ITbAppUserService;
import com.xj.api.config.jwt.JwtHelper;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.bussiness.appUser.entity.TbAppUser;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author Mars
 * @since 2017-08-24
 */
@RestController
@RequestMapping("appUser")
public class TbAppUserController extends BaseController{
	
	@Autowired
	private ITbAppUserService appUserService;
	
	@GetMapping("/list.auth")
	@ApiOperation(value = "  列表", httpMethod = "GET", notes = "  列表")
	public AbstractBean tbappuserList(@RequestParam(required=false,defaultValue="0") Integer pageNum,@RequestParam(required=false,defaultValue="10") Integer pageSize){
		return json(appUserService.selectMapsPage(new Page<TbAppUser>(pageNum, pageSize), new EntityWrapper<TbAppUser>()));
	}
	
	@GetMapping("/{phone}.auth")
	@ApiOperation(value = "获取用户详情詳情", httpMethod = "GET", notes = "根据用户名获取用户詳情")
	public AbstractBean tbappuserContent(@PathVariable Integer phone){
		return json(appUserService.selectOne(new EntityWrapper<TbAppUser>().eq("phone", phone)));
	}
	
	
	@PostMapping("/appLogin")
	@ApiOperation(value = "登陆", httpMethod = "POST", notes = "用户登录，用户名（phone）和密码（password）")
	public AbstractBean appLogin(@RequestParam String username ,@RequestParam String password){
		Map<String,Object> map=new HashMap<String,Object>();
		TbAppUser user = appUserService.selectOne(new EntityWrapper<TbAppUser>().eq("username", username).eq("password", password));
		if(user==null){
			return fail("账户或者密码不正确","-100");
		}else {
			if(!user.getPassword().equals(password)){
				return fail("账户或者密码不正确","-110");
			}
			String token=JwtHelper.createJWT(user.getUsername(),user.getPassword(), 3000000);
			map.put("token", token);
			map.put("loginUser", user);

			return json(map);
		}

	}
	
	@PostMapping("/save")
	@ApiOperation(value = "注册", httpMethod = "POST", notes = "注册用户")
	public AbstractBean add(TbAppUser tbappuser){
		if(tbappuser.getUsername().equals(null)&&tbappuser.getUsername().equals("")) {
			return fail("用户名不能为空","-102");
		}
		if(tbappuser.getPassword().equals(null)&&tbappuser.getPassword().equals("")) {
			return fail("新密码不能为空","-102");
		}
		if(!appUserService.insert(tbappuser)){
		return error();
	    }
		return success();
	}
	
	
	
	@PostMapping("/updatePassword")
	@ApiOperation(value = "修改密码", httpMethod = "POST", notes = "修改密码")
	public AbstractBean updatePassword(String username ,String oldPassword,String newPassword){
		
		if(username.equals(null)&&username.equals("")) {
			return fail("用户名不能为空","-102");
		}
		if(newPassword.equals(null)&&newPassword.equals("")) {
			return fail("新密码不能为空","-102");
		}
		
		
		TbAppUser user = appUserService.selectOne(new EntityWrapper<TbAppUser>().eq("username", username).eq("password", oldPassword));
		if(user==null) {
			return fail("用户不存在","-102");
		}else{
			user.setPassword(newPassword);
			appUserService.updateById(user);
		}
		return success();
	}
	
	
	@DeleteMapping("{id}/delete")
	@ApiOperation(value = "  删除", httpMethod = "POST", notes = " 删除")
    public AbstractBean delete(@PathVariable(required=true) Integer id) {	
		if(!appUserService.deleteById(id)){
		return error();
		}
		return success();
    }
}
